<?php
	$validation_rules = array(
        'programme_title_cn' => array(
            'maxlength' => 255
        ),
        'programme_title_en' => array(
            'maxlength' => 255
        ),
       	"title" => array(
			"maxlength" => '255'
		),
		"onair_episode_no" => array(
            'number' => true,
			"maxlength" => 9
		),
		'online_date_time[]'=>array(
			"required" => true,
		),
		'offline_date_time[]'=>array(
			"required" => true,
		)
	);

?>

<?php

	$videoData  =   isset($data['video']) ? $data['video'] : '';
	$video_subscription  =   isset($data['video_subscription']) ? $data['video_subscription'] : '';
	$video_ppv  =   isset($data['video_ppv']) ? $data['video_ppv'] : '';
    $tag_list   =   isset($data['tag_list']) ? $data['tag_list'] : '';
    $episode_no =   isset($data['eps']) ? $data['eps'] : '';
	$publish = isset($data['unpublish']) ? $data['unpublish'] : 0;
    $from    = isset($data['from']) ? $data['from'] : '';
    $concurrent_time = isset($data['concurrent_time']) ? $data['concurrent_time'] : json_encode(array());
    $deleted_chapters = isset($data['deleted_chapters']) ? $data['deleted_chapters'] : '';
    $episode = isset($data['episode']) ? $data['episode'] : "";
    $programme = isset($data['programme']) ? $data['programme'] : "";
    $video_allow_tag = isset($data['video_allow_tag']) ? $data['video_allow_tag'] : "";
    $chapter_allow_tag = isset($data['chapter_allow_tag']) ? $data['chapter_allow_tag'] : "";
    $init_tag_list = isset($data['init_tag_list']) ? $data['init_tag_list'] : array();
    $cast_list = isset($data['cast_list']) ? $data['cast_list'] : array();
    $chapter_list = isset($data['chapter_list']) ? $data['chapter_list'] : array();
    $form_action = ($act === 'view') ? "chapter_switch.html?type=chapter_marking" : "update_chapter.html?act=".$act;
    $video_file =  isset($videoData['source_url']) ?  preg_replace('/\*.+$/', '', $videoData['source_url']) : '';
    $video_id   =  isset($videoData['source_url']) ? preg_replace('/^.+\*/', '', $videoData['source_url']) : '';
    $chapter_casts = isset($data['casts']['chapter']) ? $data['casts']['chapter'] : array();
    $init_chapter_casts = isset($data['init_casts']['chapter']) ? $data['init_casts']['chapter'] : array();
    $chapter_category = isset($data['tvb_category_list']) ? $data['tvb_category_list'] : array();
    $init_chapter_category = isset($data['init_tvb_category_list']) ? $data['init_tvb_category_list'] : array();
	$chapter_duration  =   isset($data['duration']) ? $data['duration'] : '';

    $name = isset($_REQUEST['name']) ? $_REQUEST['name'] : '';
    $folder = isset($_REQUEST['folder']) ? $_REQUEST['folder'] : '';

    $act = isset($_REQUEST['act']) ? $_REQUEST['act'] : 'edit';
    $act_text = $act==='edit' ? 'Edit' : 'Create'; 

    if ($act === 'view') {
        $act_text = 'View';
    }
    
    if ($act==='view') {
        FormHelper::setReadonly(true);
        $validation_rules = array();
    }
	$_css_readonly = '';
	
	$html   =   'update_video.html?act='. ($act=='add'?'edit':$act);
    if ($act==='view') {
        $act_text = 'View';
        FormHelper::setReadonly(true);
        $validation_rules = array();
        $html=  'video_switch.html';
    }

    $num_language_zh = language::getLanguageId(AC::LANGUAGE_ZH);
    $num_language_en = language::getLanguageId(AC::LANGUAGE_EN);
    $default_language = language::getLanguageId(AC::DEFAULT_LANGUAGE);

    //pls replace it with real data
    $is_cloned = mt_rand(0,1);

?>

<div id="chapter-marking" class="mod ui-widget-content ui-helper-clearfix ui-corner-all <?php echo $act; ?>">
    <div class="hd ui-widget-header ui-helper-clearfix ui-corner-all"><h2><?php echo $act_text; ?> Video</h2></div>
    <div class="bd">

   
   
	<?php FormHelper::StartForm('ane', AC::PDES_BASE_URL.$html) ?>
    <?php FormHelper::ErrorMessages($errors) ?>
    <?php FormHelper::HiddenField('type', 'video') ?>
    <?php
    if($act!='add'){
        FormHelper::HiddenField('id', isset($videoData['id']) ? $videoData['id'] : '');
        FormHelper::HiddenField('modified_at', $videoData['modified_at']);
        FormHelper::HiddenField('old_onair_episode_no', $data['exist_onair_episode_no']);
    }
    ?>
    <?php FormHelper::HiddenField('from', $from) ?>
    <?php FormHelper::HiddenField('act',  $act) ?>
    <input type="hidden" name="programme_id" id="programme_id" value="<?php echo $videoData['programme_id']; ?>" />
    <input type="hidden"  class="duration" name='duration' value="<?php echo $chapter_duration; ?>" />
	
    	<fieldset class="ui-widget-content">
    		<legend class="ui-widget-header">Video Information</legend>
            <div id="player-wrapper">
        		<?php 
                    $image  =   $videoData['image_source_url'];
                ?>
                <div id="player">
                    <embed height="240" width="385" flashvars="height=240&amp;width=385&amp;file=<?php echo $video_file;?>&amp;id=<?php echo $video_id;?>&amp;overstretch=fit&amp;shuffle=false&amp;enablejs=true&amp;image=<?php echo $image;?>" wmode="opaque" allowfullscreen="true" allowscriptaccess="always" quality="high" name="mpl" id="mpl" src="swf/mediaplayer.swf" type="application/x-shockwave-flash"/>
                </div>
				<?php if($act=='edit') { ?>
				Replace Thumbnail Image: <input type="file" name="thumbnail" />
				<?php } ?>
                <div class="buttons">
                    <?php FormHelper::HiddenField('new_video_id', '') ?>
					<?php if($act=='edit') { ?>
					<a class="btn ui-state-default ui-corner-all replace-vd" href="#">Replace video</a>
					<?php } ?>
                    <a class="btn ui-state-default ui-corner-all download" href="<?=$videoData['download_url']?>">Right-click save as</a>
                    <a class="btn ui-state-default ui-corner-all blue" href="<?php $thumb =  isset($videoData['thumbnail_source_url'])? $videoData['thumbnail_source_url'] :  ''; echo $thumb;?>">View Thumbnail</a> 
					<input type="hidden" class="fvars" value='{"user_id":"<?php echo CommonUtils::get_user(); ?>","ptype":"programme","pid":"<?php echo $videoData['programme_id']; ?>","fid":"0","ftype":"<?php echo AC::FOLDER_TYPE_VIDEO; ?>","vid":"<?php echo $videoData['id']; ?>"}'/>
                </div>

            </div>
			<?php
                FormHelper::DropDownList($name='mdes_status', AC::$mdes_status, (int)$videoData['mdes_status'], $label='Status', false,1);
                $titles = explode("\n\t",$videoData['programme']);
                $programme_title_cn =   htmlentities($titles[0], ENT_QUOTES, 'UTF-8');
                $programme_title_en =   htmlentities($titles[1], ENT_QUOTES, 'UTF-8');
            ?>
			<?php FormHelper::HiddenField('episode_id', $data['episode_id']) ?>
            <?php FormHelper::ProtectedField($videoData['id'], 'Video ID') ?>
			<?php FormHelper::ProtectedField($videoData['programme_id'], 'Programme ID') ?>
			<?php FormHelper::ProtectedField($programme_title_cn, 'Programme Title (Chinese)') ?>
			<?php FormHelper::ProtectedField($programme_title_en, 'Programme Title (English)') ?>
            <?php FormHelper::TextField('title', $videoData['video_language'][$default_language]['title'], 
                  'Video Title', false, 'ruler input_status'); ?>

			<?php FormHelper::Separator() ?>

            <div class="form-item">
                <span class="grid-l"><label for="episode_no">On Air Episode Number :</label></span>
                <span class="grid-r">
				<?php
                $eps_no = $data['onair_episode_no'];
                if('view' === $act)
                {
                    echo '<p>'.$eps_no.'</p>'; 
                }
                else{
                    if(count($errors) > 0)
                    {
                        $eps_no = $data['init_onair_episode_no'];
                    }
                ?>
                    <input type="text" value="<?php echo $eps_no;?>" class="number" name="onair_episode_no" />
                    <?php FormHelper::HiddenField('eps',  $episode_no) ?>
                    <input type="hidden" value="0" name="edit_eps" />
                    <a href="#" class="btn ui-state-default ui-corner-all btn-edit-eps">Edit Episode</a>
                <?php }?>

                </span>
            </div>

            <?php
               // FormHelper::EditableList($videoData['casts'], array('name'=>'Casts','restrict'=>'0'), "Casts", "cast_dict_id", "cast", AC::AJAX_CAST_API_URL, false, 0, true);

                 FormHelper::DateTimePicker('first_time_onair', $videoData['first_time_onair'], 'First On Air Date Time (YYYY-MM-DD / HH:MM)');
                 
            ?>

			<?php FormHelper::Separator() ?>

<?php
                 if(count($errors)>0)
                 {
                    $seo_tag = isset($data['init_seo_tag']) ? $data['init_seo_tag'] : '' ;
					FormHelper::EditableList($seo_tag[13], $video_allow_tag[13],'Tags For SEO','', 'seo',
						AC::AJAX_TAG_API_URL.'?tag_type=13', false, 0, true);

                     //FormHelper::EditableList($videoData['init_tag'][1], $allow_tag[1],'Tags','[tags]',
                       //  'tag', AC::AJAX_TAG_API_URL, false, 0, true);
                 }
                 else
                 {
					 FormHelper::EditableList($tag_list[13], $video_allow_tag[13],'Tags For SEO','', 'seo',
						 AC::AJAX_TAG_API_URL.'?tag_type=13', false, 0, true);
                   
                    //FormHelper::EditableList($tag_list[1], $allow_tag[1],'Tags','[tags]',
                       //  'tag', AC::AJAX_TAG_API_URL, false, 0, true);
                 }
?>
<?php
FormHelper::RadioButton('video_type', array('O'=>'O-Version','I'=>'I-Version'), $videoData['video_type'], 'Video Type') ?>
<?php
				 if($data['onair_episode_no']){
					 FormHelper::ProtectedField($data['week_zh_encode'],'Episode Synopsis <br>(tvbWeekly Chinese)',1);
					 FormHelper::ProtectedField($data['week_en_encode'], 'Episode Synopsis <br>(tvbWeekly English)',1);
				 }
?>
<?php 

    			FormHelper::TextArea('subtitle_cn', $videoData['subtitle_cn'], 'Subtitle (Chinese)');
    			FormHelper::TextArea('subtitle_en', $videoData['subtitle_en'], 'Subtitle (English)');
?>

        </fieldset>


<?php
    //if ($data['programme_premium_flag']) {
    // for bug 6708
    if ($data['programme_premium_flag'] && ($act=='edit' || !empty($data['video']['premium_flag']) )) {
?>


    <?php if ($data['unpublish'] == 0 || ($data['unpublish'] == 1 && isset($data['special_operation_premium']) && $data['special_operation_premium'] == TRUE)) { ?>
    <fieldset class="ui-widget-content premium_subscription">
        <legend class="ui-widget-header">Premium Key</legend>
        <?php FormHelper::CheckBox(
                    'premium_items', 
                    array(
                        array('label'=>'Subscription','value'=>'subscription','checked'=> $videoData['subscription_flag']),
                        array('label'=>'Pay-Per-View','value'=>'ppv','checked'=> $videoData['ppv_flag'])
                    ), 
                    'Premium Items',
                    false,
                    false
              ); 
        ?>
        <?php FormHelper::RadioButton('premium_flag', array(1=>'Yes',0=>'No'), $videoData['premium_flag'], 'Use for premium', false, 0) ?>
    </fieldset>
    <?php } ?>

<?php if('view' == $act)
    {
        if($videoData['subscription_flag'])
        {
?>
    <fieldset class="ui-widget-content premium_subscription <?=$_css_readonly?>">
            <legend class="ui-widget-header">Premium: Subscription</legend>
        	    <?php FormHelper::DateTimePicker('subscription_publish_datetime',  CommonUtils::formatDate($video_subscription['publish_datetime']), 'Online Date Time (YYYY-MM-DD / HH:MM)', false, 1) ?>
    		    <?php FormHelper::DateTimePicker('subscription_expire_datetime', CommonUtils::formatDate($video_subscription['expire_datetime']), 'Offline Date Time (YYYY-MM-DD / HH:MM)', false, 1) ?>
                <?php //FormHelper::RadioButton('subscription_free', array(1=>'Yes',0=>'No'), $video_subscription['free'], 'Free', false, 0); ?>
                <?php FormHelper::Separator();?>
                <!--
                <div class="form-item" style="padding-bottom:0px">
                    <span class="grid-l">
                        <label>Country Allow / Deny :</label>
                    </span>
                    <span class="grid-r">
<?php
            /*
                    $radioAllow =   $radioDeny  =   $select =   NULL;
                    if('allow' == $video_subscription['subscription_country_allow_deny'])
                    {
                        $radioAllow = 'checked="checked" ';
                    }
                    else
                    {
                        $radioDeny = 'checked="checked" ';
                    }

                    if (empty($select))     $select =   array();
                    else    $select     =   explode(',', $select);
                    
                    $disabled = ('view' === $act)?' disabled="disabled"':'';
             */
?>
                    <label><input type="radio" name="subscription_country_allow_deny" value="allow" 
                        <?php //echo $radioAllow . $disabled; ?>/>Allow </label>
                    <label><input type="radio" name="subscription_country_allow_deny" value="deny" 
                        <?php //echo $radioDeny . $disabled; ?>/>Deny </label>
                    </span>
                </div>
                -->
                <?php
            /*
                $sub_country = isset($video_subscription['subscription_country']) ? json_decode($video_subscription['subscription_country']) : array();
                FormHelper::CountryList('subscription_country','',
                    $sub_country,
                    array(
                        'stat'=>AC::$country_stat,
                        'service'=>AC::$country_service,
                        'mandatory'=>1
                    )
                )
             */
                ?>
                
        </fieldset>
    <?php }
      if($videoData['ppv_flag']){
?>
        <fieldset class="ui-widget-content premium_ppv">
            <legend class="ui-widget-header">Premium: Pay-Per-View</legend>
        	    <?php FormHelper::DateTimePicker('ppv_publish_datetime',CommonUtils::formatDate($video_ppv['publish_datetime']), 'Online Date Time (YYYY-MM-DD / HH:MM)', false, 1) ?>
    		    <?php FormHelper::DateTimePicker('ppv_expire_datetime', CommonUtils::formatDate($video_ppv['expire_datetime']), 'Offline Date Time (YYYY-MM-DD / HH:MM)', false, 1) ?>
                <!--
                <div class="form-item" style="padding-bottom:0px">
                    <span class="grid-l">
                        <label>Country Allow / Deny :</label>
                    </span>
                    <span class="grid-r">
<?php
          /*
                    $radioAllow =   $radioDeny  =   $select =   NULL;
                    if('allow' == $video_ppv['ppv_country_allow_deny'])
                    {
                        $radioAllow = 'checked="checked" ';
                    }
                    else
                    {
                        $radioDeny = 'checked="checked" ';
                    }
                    if (empty($select))     $select =   array();
                    else    $select     =   explode(',', $select);
                    
                    $disabled = ('view' === $act)?' disabled="disabled"':'';
           */
?>
                    <label><input type="radio" name="ppv_country_allow_deny" value="allow" 
                        <?php //echo $radioAllow . $disabled; ?>/>Allow </label>
                    <label><input type="radio" name="ppv_country_allow_deny" value="deny" 
                        <?php //echo $radioDeny . $disabled; ?>/>Deny </label>
                    </span>
                </div>
                -->
<?php
          /*
                $ppv_country = isset($video_ppv['ppv_country']) ? json_decode($video_ppv['ppv_country']) : array();
                FormHelper::CountryList('ppv_country','',
                    $ppv_country,
                    array(
                        'stat'=>AC::$country_stat,
                        'service'=>AC::$country_service,
                        'mandatory'=>1
                    )
                )
           */
                ?>

        </fieldset>
<?php } } else
            {?>
        <fieldset class="ui-widget-content premium_subscription <?=$_css_readonly?>">
            <legend class="ui-widget-header">Premium: Subscription</legend>
        	    <?php FormHelper::DateTimePicker('subscription_publish_datetime',  CommonUtils::formatDate($video_subscription['publish_datetime']), 'Online Date Time (YYYY-MM-DD / HH:MM)', false, 1) ?>
    		    <?php FormHelper::DateTimePicker('subscription_expire_datetime', CommonUtils::formatDate($video_subscription['expire_datetime']), 'Offline Date Time (YYYY-MM-DD / HH:MM)', false, 1) ?>
                <?php //FormHelper::RadioButton('subscription_free', array(1=>'Yes',0=>'No'), $video_subscription['free'], 'Free', false, 0); ?>
                <?php FormHelper::Separator();?>
                <!--
                <div class="form-item" style="padding-bottom:0px">
                    <span class="grid-l">
                        <label>Country Allow / Deny :</label>
                    </span>
                    <span class="grid-r">
<?php
            /*
                    $radioAllow =   $radioDeny  =   $select =   NULL;
                    if('allow' == $video_subscription['subscription_country_allow_deny'])
                    {
                        $radioAllow = 'checked="checked" ';
                    }
                    else
                    {
                        $radioDeny = 'checked="checked" ';
                    }

                    if (empty($select))     $select =   array();
                    else    $select     =   explode(',', $select);
                    
                    $disabled = ('view' === $act)?' disabled="disabled"':'';
             */
?>
                    <label><input type="radio" name="subscription_country_allow_deny" value="allow" 
                        <?php //echo $radioAllow . $disabled; ?>/>Allow </label>
                    <label><input type="radio" name="subscription_country_allow_deny" value="deny" 
                        <?php //echo $radioDeny . $disabled; ?>/>Deny </label>
                    </span>
                </div>
                -->
                <?php
            /*
                $sub_country = isset($video_subscription['subscription_country']) ? json_decode($video_subscription['subscription_country']) : array();
                FormHelper::CountryList('subscription_country','',
                    $sub_country,
                    array(
                        'stat'=>AC::$country_stat,
                        'service'=>AC::$country_service,
                        'mandatory'=>1
                    )
                )
             */
                ?>
                
        </fieldset>

        <fieldset class="ui-widget-content premium_ppv">
            <legend class="ui-widget-header">Premium: Pay-Per-View</legend>
        	    <?php FormHelper::DateTimePicker('ppv_publish_datetime',CommonUtils::formatDate($video_ppv['publish_datetime']), 'Online Date Time (YYYY-MM-DD / HH:MM)', false, 1) ?>
    		    <?php FormHelper::DateTimePicker('ppv_expire_datetime',CommonUtils::formatDate($video_ppv['expire_datetime']), 'Offline Date Time (YYYY-MM-DD / HH:MM)', false, 1) ?>
                <!--
                <div class="form-item" style="padding-bottom:0px">
                    <span class="grid-l">
                        <label>Country Allow / Deny :</label>
                    </span>
                    <span class="grid-r">
<?php
            /*
                    $radioAllow =   $radioDeny  =   $select =   NULL;
                    if('allow' == $video_ppv['ppv_country_allow_deny'])
                    {
                        $radioAllow = 'checked="checked" ';
                    }
                    else
                    {
                        $radioDeny = 'checked="checked" ';
                    }
                    if (empty($select))     $select =   array();
                    else    $select     =   explode(',', $select);
                    
                    $disabled = ('view' === $act)?' disabled="disabled"':'';
             */
?>
                    <label><input type="radio" name="ppv_country_allow_deny" value="allow" 
                        <?php //echo $radioAllow . $disabled; ?>/>Allow </label>
                    <label><input type="radio" name="ppv_country_allow_deny" value="deny" 
                        <?php //echo $radioDeny . $disabled; ?>/>Deny </label>
                    </span>
                </div>
                -->
                <?php
            /*
                $ppv_country = isset($video_ppv['ppv_country']) ? json_decode($video_ppv['ppv_country']) : array();
                FormHelper::CountryList('ppv_country','',
                    $ppv_country,
                    array(
                        'stat'=>AC::$country_stat,
                        'service'=>AC::$country_service,
                        'mandatory'=>1
                    )
                )
             */
                ?>

        </fieldset>
<?php }}?>

        <fieldset class="ui-widget-content">
            <legend class="ui-widget-header">Global Information</legend>
        	    <?php FormHelper::DateTimePicker('online_date_time',  $videoData['publish_datetime'], 'Online Date Time (YYYY-MM-DD / HH:MM)', false, 1) ?>
    		    <?php FormHelper::DateTimePicker('offline_date_time', $videoData['expire_datetime'], 'Offline Date Time (YYYY-MM-DD / HH:MM)', false, 1) ?>
                
                <div class="form-item" style="padding-bottom:0px">
                    <span class="grid-l">
                        <label>Country Allow / Deny :</label>
                    </span>
                    <span class="grid-r">
<?php
                    $radioAllow =   $radioDeny  =   $select =   NULL;
                    if('allow' == $videoData['global_country_allow_deny'])
                    {
                        $radioAllow = 'checked="checked" ';
                    }
                    else
                    {
                        $radioDeny = 'checked="checked" ';
                    }
                    if (empty($select))     $select =   array();
                    else    $select     =   explode(',', $select);
                    
                    $disabled = ('view' === $act)?' disabled="disabled"':'';
?>
                    <label><input type="radio" name="country_allow_deny" value="allow" 
                        <?php echo $radioAllow . $disabled; ?>/>Allow </label>
                    <label><input type="radio" name="country_allow_deny" value="deny" 
                        <?php echo $radioDeny . $disabled; ?>/>Deny </label>
                    </span>
                </div>
<?php
    $global_country = isset($videoData['global_country']) ? json_decode($videoData['global_country']) : array();
    FormHelper::CountryList('country','',
        $global_country,
        array(
            'stat'=>AC::$country_stat,
            'service'=>AC::$country_service,
            'mandatory'=>1
        )
    )
?>
                <div class="form-item">
                    <span class="grid-l">
                        <label>Copyright :</label>
                    </span>
                    <span class="grid-r">
                    <?php
                    $copyright = $videoData['copyright'];
                    if('view' === $act)
                    {
                        echo '<p>'.$copyright.'</p>'; 
                    }
                    else{
                        if('TVB' === $copyright || empty($copyright) )
                        {
                            echo '<label><input type="radio" name="copyright" checked="checked" value="TVB" />TVB </label><br />';
                            echo ' <label><input type="radio" name="copyright" />Others: </label>
                                   <input type="text" name="copyright_owner" disabled="disabled"/>';
                        }
                        else
                        {
                            echo '<label><input type="radio" name="copyright" value="TVB" />TVB </label><br />';
                            echo ' <label><input type="radio" checked="checked" name="copyright" />Others: </label>
                                <input type="text" name="copyright_owner" value="'.$copyright.'"/>'; 
                        }
                    ?>

                    <?php }?>
                    </span>
                </div>

        </fieldset>

        <?php 
            if($act != 'view')
            {
        ?>
    	<fieldset id="chapter_modal" class="template chapter ui-widget-content">
    		<legend class="ui-widget-header">Chapter Information</legend>
            <a class="btn close ui-state-default ui-corner-all" title="Delete" href="#"><span class="ui-icon ui-icon-closethick"/></a>
    		<?php
				FormHelper::HiddenField('chapter_id[]', 'new');
				FormHelper::HiddenField('exist_thumbnail_image[]', '');
				FormHelper::TextField('chapter_title_cn[]', '','Chapter Title (Chinese)', false, 'ruler input_status', 2);
				FormHelper::TimeHhMmSs(array('chapter_in_time_hour[]', 'chapter_in_time_minute[]', 'chapter_in_time_second[]'), array('','',''), 'In Time (hh:mm:ss)',false,1);
    			FormHelper::TextField('chapter_title_en[]', '','Chapter Title (English)', false, 'ruler input_status', 2);
				FormHelper::TimeHhMmSs(array('chapter_out_time_hour[]', 'chapter_out_time_minute[]', 'chapter_out_time_second[]'), array('','',''), 'Out Time (hh:mm:ss)',false,1);
    			FormHelper::TextArea('chapter_description_cn[]', '', 'Description (Chinese)');
    			FormHelper::TextArea('chapter_description_en[]', '', 'Description (English)');
            ?>

            <?php
            FormHelper::EditableList(array(), array('name'=>'Casts','restrict'=>'0'), "Casts", "", "cast", AC::AJAX_CAST_API_URL, false, 0, true);

            $arr = array(
                1 => array ('id' => 1, 'name' => 'Tags', 'restrict' => '0' ),
                3 => array ('id' => 3, 'name' => 'chapter nature', 'restrict' => '1' ),
                4 => array ('id' => 4, 'name' => 'liberal', 'restrict' => '0' ),
                13 => array ('id' => 13, 'name' => 'seo', 'restrict' => '0' )
            );

            $ajax_api = "services/get_list.php";
            FormHelper::EditableList(array(), array('name'=>'tvbcom_cat_id','restrict' =>'1'), "TVB.COM Category", "", "tvb.com_cat", $ajax_api);

            FormHelper::Separator();

			//if($data['_g']['all'] || $data['_g']['liberal']){
            //if(in_array('Liberal_ChapterGetAction',$data['_up'])){
			    FormHelper::EditableList(array(), $chapter_allow_tag[1], 'Tags' ,'[1]', 'tag', AC::AJAX_TAG_API_URL, false, 0, true);
               // FormHelper::EditableList(array(), $chapter_allow_tag[4], 'Liberal Tags','[4]', 'liberal_tag',
                 //   AC::AJAX_TAG_API_URL.'?tag_type=4',false,0,true);
            //}

            if ($data['_g']['all'] || $data['_g']['liberal'] || $data['_g']['proofread']) {
            ?>
           <div class="form-item">
               <span class="grid-l"><label>Liberal Tags</label> :</span>
               <span class="grid-r editable-list combo">
                   <ul class="ui-widget-content"></ul>
                   <input type="text" value="" class="text editable" /><a href="#"
                   class="btn add ui-state-default ui-corner-all">Add</a>
                   <a href="services/get_list.php" class="btn ui-state-default ui-corner-all addpop">Select</a>
                   <input type="hidden" value="" class="id" />
                   <input type="hidden" value="liberal_tag" class="type" />
               </span>
           </div>
            <?php
            }
    		//	FormHelper::TextArea('location[]', '', 'Location'); 
  			    FormHelper::FileUpload('screen_capture[]', 'Thumbnail Image', '', '', false, 1);
    			#FormHelper::FileUpload('screen_capture[]', 'Thumbnail Image'); 
    			FormHelper::HiddenField('attached_info[]', ''); 
			?>

 		    <?php if ($data['episode_id']) { ?>
            <div class="form-item form-item-1c">
                <span class="grid-l">Info</span>
                <span class="grid-r">
                        <div class="info-list"></div>
                    	<?php FormHelper::Button('attach_info_type', 'Attach') ?> 
                </span>
            </div>
			<?php } ?>

        </fieldset>
        <?php } ?>
<?php
            if(!empty($chapter_list))
            {
				foreach ($chapter_list as $chapter)/*{{{*/
				{
					$chapter_title = explode("\n\t",$chapter->chapter_title);
					$chapter->zh_chapter_title = $chapter_title[0];
					$chapter->en_chapter_title = isset($chapter_title[1]) ? $chapter_title[1] : '';
				    $chapter_description = explode("\n\t", $chapter->chapter_description);
					$chapter->zh_description = $chapter_description[0];
					$chapter->en_description = isset($chapter_description[1]) ? $chapter_description[1] : '';
					$chapter->in_time  = CommonUtils::sec_to_num($chapter->in_time);
					$chapter->out_time = CommonUtils::sec_to_num($chapter->out_time);
					$cast_list = json_decode($chapter->casts,true);	
					$info_list = array();
					if (!empty($chapter->infos))
					{
						$json_info_list = json_decode($chapter->infos, true);
						foreach ($json_info_list as $infotype=>$infos)
						{
							foreach ($infos as $id)
							{
								$info_list[$infotype][] = array('name'=>CommonUtils::get_infos_name($infotype, $id),'id'=>$id);
							}
						}
					}

					$tag_list = array();
					if (count($errors) > 0)
					{

                    	if (!empty($init_tag_list))
						{
							$tag_list[1] = isset($init_tag_list[$chapter->id][1]) ? $init_tag_list[$chapter->id][1] : array();
							$tag_list[3] = isset($init_tag_list[$chapter->id][3]) ? $init_tag_list[$chapter->id][3] : array();
							$tag_list[4] = isset($init_tag_list[$chapter->id][4]) ? $init_tag_list[$chapter->id][4] : array();
						}
						else
						{
							$tag_list[1] = array();
							$tag_list[3] = array();
							$tag_list[4] = array();
						}
					}
					else
					{
						if (!empty($chapter->tags))
						{
							$tag = new tag();
							$tag_all = $tag->getTagList($chapter->tags);
							foreach ($tag_all as $tag)
							{
								$tag_list[$tag->typeid][] = array(
									'system_label'	=> $tag->system_label,
									'id'			=> $tag->id
								);
							}
							#print_r($tag_list);
							//handle null tag
							foreach ($chapter_allow_tag as $tag_id=>$tag_value)
							{
								if (!isset($tag_list[$tag_id]))
								{
									$tag_list[$tag_id] = array();
								}
							}

						}
						else
						{
							foreach ($chapter_allow_tag as $tag_id=>$tag_value)
							{
								$tag_list[$tag_id] = array();
							}
						}
					}/*}}}*/
?>


 	<fieldset class="chapter ui-widget-content">
    		<legend class="ui-widget-header">Chapter Information</legend>
			<?php
			if (empty($data['chapter_publishLog']->non_premium) ||
				(!empty($data['chapter_publishLog']->non_premium) && $data['chapter_publishLog']->non_premium->status == AC::STATUS_UNPUBLISH && empty($data['chapter_publishLog']->premium)) ||
				(!empty($data['chapter_publishLog']->non_premium) && $data['chapter_publishLog']->non_premium->status == AC::STATUS_UNPUBLISH && !empty($data['chapter_publishLog']->premium) && $data['chapter_publishLog']->premium->status == AC::STATUS_UNPUBLISH_PREMIUM)) {
				?>
				<a class="btn close ui-state-default ui-corner-all" title="Delete" href="#"><span class="ui-icon ui-icon-closethick"/></a>
				<?php
			}
			?>
			<?php
				FormHelper::HiddenField('chapter_id[]', $chapter->id);
				FormHelper::HiddenField('exist_thumbnail_image[]',  $chapter->source_url);
                FormHelper::TextField('chapter_title_cn[]', $chapter->zh_chapter_title,
                                      'Chapter Title (Chinese)', false, 'ruler input_status', 2);
               
                FormHelper::TimeHhMmSs(array(
                    'chapter_in_time_hour[]','chapter_in_time_minute[]', 'chapter_in_time_second[]'), 
                    array($chapter->in_time[0],$chapter->in_time[1],$chapter->in_time[2]), 
                                        'In Time (hh:mm:ss)', false, 1);
                FormHelper::TextField('chapter_title_en[]', $chapter->en_chapter_title,
                                      'Chapter Title (English)', false, 'ruler input_status', 2);
                FormHelper::TimeHhMmSs(array(
                    'chapter_out_time_hour[]','chapter_out_time_minute[]', 'chapter_out_time_second[]'), 
                    array($chapter->out_time[0],$chapter->out_time[1],$chapter->out_time[2]), 
                                        'Out Time (hh:mm:ss)', false, 1);
                FormHelper::TextArea('chapter_description_cn[]', $chapter->zh_description,'Chapter Description(Chinese)');
                FormHelper::TextArea('chapter_description_en[]', $chapter->en_description,'Chapter Description(English)');

            ?>
			
<?php
			$arr = array();
			$chapter_id = $chapter->id;
			$arr_cast = array(
					$chapter_id=>'Cast(s)'
			);

           // $chapter_cast = isset($chapter_cast[$chapter->id]) ? $chapter_cast[$chapter->id] : array();
            //FormHelper::EditableList($chapter_cast, array('name'=>'Casts','restrict'=>'0'), "Casts", "cast_dict_id[{$chapter->id}]", "cast", AC::AJAX_CAST_API_URL, false, 0, true);

       
            $ajax_api = "services/get_list.php";
            if(count($errors)>0)
            {
                $chapter_cast = isset($init_chapter_casts[$chapter->id]) ? $init_chapter_casts[$chapter->id] : array();
                FormHelper::EditableList($chapter_cast, array('name'=>'Casts','restrict'=>'0'), "Casts", "cast_dict_id[{$chapter->id}]", "cast", AC::AJAX_CAST_API_URL, false, 0, true);

                $category = array_key_exists($chapter->id,$init_chapter_category) ? $init_chapter_category[$chapter->id] : array();
                FormHelper::EditableList($category, array('name'=>'tvbcom_cat_id','restrict' =>'1'), 
                "TVB.COM Category", 'tvbcom_cat_id['.$chapter->id.']', "tvb.com_cat", $ajax_api,false);
            }
            else
            {
                $chapter_cast = isset($chapter_casts[$chapter->id]) ? $chapter_casts[$chapter->id] : array();
                FormHelper::EditableList($chapter_cast, array('name'=>'Casts','restrict'=>'0'), "Casts", "cast_dict_id[{$chapter->id}]", "cast", AC::AJAX_CAST_API_URL, false, 0, true);

                $category = array_key_exists($chapter->id,$chapter_category) ? $chapter_category[$chapter->id] : array();
                FormHelper::EditableList($category, array('name'=>'tvbcom_cat_id','restrict' =>'1'), 
                "TVB.COM Category", 'tvbcom_cat_id['.$chapter->id.']', "tvb.com_cat", $ajax_api,false);
            }

			FormHelper::Separator();

           FormHelper::EditableList($tag_list[1], $chapter_allow_tag[1], 'Tags' ,'['.$chapter_id.']', 'tag', 
											 AC::AJAX_TAG_API_URL.'?=tag_type=1', false, 0, true);
/*            
           if($data['_g']['all'] || $data['_g']['liberal']){
					FormHelper::EditableList($tag_list[4], $chapter_allow_tag[4], 'Liberal Tags','['.$chapter_id.']', 'liberal_tag', AC::AJAX_TAG_API_URL.'?tag_type=4', false, 0, true);
			}
 */
?>
<?php if($data['_g']['all'] || $data['_g']['liberal'] || $data['_g']['proofread']){ ?>
           <div class="form-item">
               <span class="grid-l"><label>Liberal Tags</label> :</span>
               <span class="grid-r editable-list combo">
                   <ul class="ui-widget-content">
<?php
            if(!empty($tag_list[4]))
            {
                foreach($tag_list[4] as $tag)
                {
                    $label = $tag['system_label'];
                    $tag_id = $tag['id'];
                    echo('
                        <li>
                            <span class="tag">'.$label.'</span>
                            <input type="hidden" value="'.$tag_id.'" 
                                   name="tag['.$chapter_allow_tag[4]['id'].']['.$chapter_id.'][]"/>
                            <a href="#" class="btn-del">del</a>
                        </li>                        
                        ');
                }
            }
?>
                   </ul>
                   <input type="text" value="" class="text editable" /><a href="#"
                   class="btn add ui-state-default ui-corner-all">Add</a>
                   <a href="services/get_list.php" class="btn ui-state-default ui-corner-all addpop">Select</a>
                   <input type="hidden" value="" class="id" />
                   <input type="hidden" value="liberal_tag" class="type" />
               </span>
           </div>
<?php }?>

				
            <?php
             //   $location_encode = $chapter->location;

    		//	FormHelper::TextArea('location[]',$location_encode, 'Location'); 
    			FormHelper::FileUpload('screen_capture[]', 'Thumbnail Image', $chapter->thumbnail_url, $chapter->source_url,false,1); 
    			FormHelper::HiddenField('attached_info[]', $chapter->infos); 
			?>

<?php if($data['episode_id']) { ?>
            <div class="form-item form-item-1c">
                <span class="grid-l">Info</span>
                <span class="grid-r">
                    <div class="info-list"><ul class="ui-widget-content">
<?php
				foreach ($info_list as $type=>$infos)
				{
					foreach ($infos as $info_detail)
					{
                        $info_encode = $info_detail['name'];

?>
    <li>
    <span class="type"><?php echo AC::$infoName[$type]['label'];?></span> - <span class="name"><?php echo $info_encode;?></span>
    <?php if($act != 'view'){?> 
    <a class="infotype_details ui-icon ui-icon-search" href="pms_partial.html?act=view&type=<?php echo strtolower(AC::$infoName[$type]['label']);?>&id=<?php echo  $info_detail['id'];?>"/>
    <a href="#<?php echo $info_detail['id'];?>" class="btn-del">del</a><?php } ?>
    </li>
<?php
					}
				}
?>
                    </ul></div>
                    <?php ($act != 'view') ? FormHelper::Button('attach_info_type', 'Attach') : "";?>
                </span>
            </div>
<?php } ?>

        </fieldset>
<?php
                    }
				}
?>




		<div id="chapter_placeholder"></div>
        
        <?php FormHelper::HiddenField('deleted_chapters', $deleted_chapters); ?>

        <span class="buttons">
		<?php FormHelper::Button('add-chapter', 'Add chapter', 'add-chapter') ?> 
        </span>

		
    	
         <?php
            if($act != 'add') { ?>
                 <fieldset class="ui-widget-content">
                     <legend class="ui-widget-header">Record Status and Access Log</legend>
                        <?php isset(AC::$op_status[$videoData['status']]) ?
                            FormHelper::ProtectedField(AC::$op_status[$videoData['status']], 'Status') :
                            FormHelper::ProtectedField('', 'Status')?>
                         <div class="form-item">&nbsp;</div>
                         <?php FormHelper::ProtectedField($videoData['created_by'], 'Created By') ?>
                         <?php FormHelper::ProtectedField($videoData['created_at'], 'Created At') ?>
                         <?php FormHelper::ProtectedField($videoData['modified_by'], 'Modified By') ?>
                         <?php FormHelper::ProtectedField($videoData['modified_at'], 'Modified At') ?>
                 </fieldset>
         <?php } ?>

        <?php FormHelper::DisplayPublishLog($data['publishLog']); ?>
        <span class="buttons">
        <?php
                    switch ($act) {
                    case 'edit':
                        FormHelper::SubmitButton('update', 'Update');
                        FormHelper::ResetButton('reset', 'Reset');
                        FormHelper::Button('back', 'Back to list');
                        break;
                    case 'view':
                        /** modified by vanni for bug no:260 **/
                        if($publish ==1){
                            CommonUtils::showUnpublishButton($data, $data['_g']);
                        }else if($data['video']['status'] == AC::STATUS_NEW){
                            FormHelper::SubmitButton('edit', 'Edit');
                            CommonUtils::showDeleteButton($data['publishLog']);
                        }
                        else if($data['video']['status'] ==  AC::STATUS_DELETED){
                            FormHelper::SubmitButton('recover', 'Recover');
                        }
                        FormHelper::Button('back', 'Back to list');
                        break;
                    case 'add':
                        FormHelper::SubmitButton('submit', 'Create');
                        FormHelper::ResetButton('reset', 'Reset');
                        break;
                    }
		?>
        </span>
		
        <?php FormHelper::EndForm() ?>
    </div>
</div>

<div id="infotype_search" class="popup">
	<?php FormHelper::StartForm('find_infotype', 'services/get_infos.php') ?>
	<?php FormHelper::HiddenField('selected', '') ?>
    <?php FormHelper::HiddenField('episode_id', $data['episode_id']); ?>
	<?php FormHelper::DropDownList('infotype', array('all'=>'All', 'address'=>'Address', 'recipe'=>'Recipe', 'song'=>'Song', 'biography'=>'Biography', 'award'=>'Award', 'product'=>'Product', 'qa'=>'Q&A', 'general_info'=>'General Info'), 'all', "Info Type") ?>
    <?php FormHelper::EndForm() ?>
	<div class="results"> Pick an info type to search </div>
</div>

<script>
    TVB.Pdes.oValidation = {
        form: document.forms['ane'],
        rules: <?php echo json_encode($validation_rules); ?>
    };
    TVB.Pdes.Vars.isCloned = <?php echo $is_cloned ? 'true' : 'false'; ?>;
</script>




